home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DBPCXL15.ARJ / DEMOS.ARJ / SHOWPCX.C < prev    next >
C/C++ Source or Header  |  1992-01-26  |  10KB  |  302 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dos.h>
  4. #include <alloc.h>
  5. #include <string.h>
  6. #include <conio.h>
  7. #include <errno.h>
  8.  
  9. #include "scancode.h"  /* used in the get_command() function */
  10. #include "pcxlib.h"
  11. #include "vidlib.h"  /* uses Vinit(), VGAmode(), and Vclose() */
  12. #include "vesa.h"
  13.  
  14. /* Uncomment the next line to use VESA mode 0x101, if needed
  15.    (it makes my screen jitter, so I don't use it) */
  16. /* #define VESA101 */
  17.  
  18. int fx=0, fy=0, changed=1, quitflag=0; /* fx,fy = new from coordinates */
  19. int tx, ty, dx, dy;
  20. PCXF *pf;
  21.  
  22. void get_command(void);
  23. void Check_PCXerror(void);  /* if an error, print msg and exit */
  24. char fname[80];
  25. char s[100];
  26.  
  27. /*///////////////////////////////////////////////////////////////////////////
  28. //            m a i n                                                      //
  29. ///////////////////////////////////////////////////////////////////////////*/
  30. void main(int argc, char *argv[])
  31. {
  32.    int   ofx, ofy; /* old coordinates. needed to detect a change */
  33.    int  mode=0;
  34.  
  35.    sprintf(s,"PCXLIB v%s Showpcx    by Dave Boynton\n", PCXLIB_VERSION);
  36.  
  37.    /*********************************/
  38.    /*   Initialization              */
  39.    /*********************************/
  40.    /* Sanity check */
  41.    if ((argc < 2)||(*argv[1] == '/')||(*argv[1] == '-')){
  42.       printf("%s\nYou must specify a filename to show.\n",s);
  43.       printf("Once shown, use ESC to quit and clear the screen, \n"
  44.              "or 'r' to quit without clearing. For large files, \n"
  45.              "use the arrows, PGUP, PGDN, Home, and End keys to move.\n");
  46.       exit(1);
  47.    }
  48.    strncpy(fname,argv[1],80); fname[79]='\0';
  49.  
  50.    /* Open debugging file */
  51.    #ifdef DEBUG
  52.    Start_pcxdebug("showpcx.dbg");
  53.    if (pcxdebug) {
  54.       fprintf(pcxdebug,"\nMAIN: arguments are: 0=%s 1=%s\n", argv[0], argv[1]);
  55.       fprintf(pcxdebug,"   sizeof(PCXF)=%u %xh, sizeof(PCXH)=%u %xh\n",
  56.                     sizeof(PCXF), sizeof(PCXF), sizeof(PCXH), sizeof(PCXH));
  57.    }
  58.    #endif
  59.  
  60.    /* Open pcx file */
  61.    pf=fopenPCXr(fname); /* also reads header */
  62.    Check_PCXerror();
  63.    if (pf->type != PCX256colors ) {
  64.       printf("Wrong pcx file, expected a 256 color file.\n");
  65.       exit(1);
  66.    }
  67.  
  68.    /* Ready VGA display mode 13h or VESA modes 100h or 101h */
  69.    if ( !isVGA() ) {
  70.       printf("This test program is for VGAs and VESAs only.\n");
  71.       fclosePCX(pf);
  72.       exit(1);
  73.    }
  74.  
  75.    Vinit();
  76.    #ifdef VESA101
  77.    if ( isVESA() && (pf->l > 400) ) {
  78.       if (VESAmode(0x101,pf->palette) != 0)
  79.          mode=0x101;
  80.    }
  81.    #endif
  82.    if ( !mode && isVESA() /* && ((pf->l > 200) || (pf->w > 320)) */ ) {
  83.       if (VESAmode(0x100,pf->palette) != 0)
  84.          mode=0x100;
  85.    }
  86.    if ( !mode ) {
  87.       if (VGAmode(0x13,pf->palette) == 0) {
  88.          printf("VGA mode change failed.\n");
  89.          fclosePCX(pf);
  90.          exit(2);
  91.       }
  92.       mode=0x13;
  93.    }
  94.    /* any other functions which set the modes, update the _screen_start,
  95.      _screen_width, and _screen_length variables would work */
  96.  
  97.    /* setup initial values */
  98.    fy=0; fx=0;
  99.    if ( pf->l < _screen_length ) {
  100.       dy=(_screen_length- pf->l)/2;
  101.       ty=fy+pf->l-1;
  102.    } else {
  103.       dy=0;
  104.       ty=_screen_length-1;
  105.    }
  106.    if ( pf->w < _screen_width ) {
  107.       dx=(_screen_width - pf->w)/2;
  108.       tx=fx+pf->w-1;
  109.    } else {
  110.       dx=0;
  111.       tx=_screen_width-1;
  112.    }
  113.  
  114.    Vgotoxy(0,BIOScrtrows-1);
  115.    putstty(s,0x1f);
  116.    Vgotoxy(0,BIOScrtrows);
  117.    putstty(fname,0x1f);
  118.    Vgotoxy(0,0);
  119.    /*********************************/
  120.    /* End intialization, main loop: */
  121.    /* loop until unknown key is hit */
  122.    /*********************************/
  123.    while (!quitflag) {
  124.       if (changed) {   /* if our coordinates have changed, redisplay */
  125.          if (pcxdebug) {
  126.             fprintf(pcxdebug,
  127.                 "_s_w=%u, _s_l=%u, dx=%u, dy=%u, fx=%u, fy=%u, "
  128.                 "tx=%u, ty=%u, PCXerror=%i\n",
  129.                  _screen_width, _screen_length, dx, dy, fx, fy, tx, ty,
  130.                  PCXerror);
  131.          }
  132.          if (fdisp8PCX( dx, dy, fx, fy, tx, ty, pf) == -1) {
  133.             quitflag=1;
  134.             break; /* return == -1 indicates an error, break out */
  135.          } else { /* everything's fine, continue loop */
  136.             changed=0;
  137.          }
  138.       } /* end if changed */
  139.       ofx=fx; ofy=fy;
  140.       get_command();
  141.       if ( (fx != ofx) || (fy != ofy) ) changed=1;
  142.    } /* end while(!quitflag) */
  143.  
  144.    /*********************************/
  145.    /* end of program                */
  146.    /*********************************/
  147.    if (quitflag == 1) {  /* if quitflag == 2 , don't reset mode! */
  148.       Vclose(); /* resets original mode */
  149.    }
  150.    fclosePCX(pf); Close_pcxdebug();
  151.    Check_PCXerror();
  152.    exit(0);
  153. }
  154. /************************************************************************/
  155. /************************************************************************/
  156.  
  157. void Check_PCXerror(void)
  158. {
  159.    if (PCXerror) {
  160.       errno=PCXerror;
  161.       perror(fname);
  162.       exit(errno);
  163.    }
  164. }
  165.  
  166.  
  167. /*//////////////////////////////////////////////////////////////////////////
  168.  
  169. ///////////////////////////////////////////////////////////////////////////*/
  170. void get_command(void)
  171. {
  172.       int inkey;
  173.  
  174.       inkey=getch();
  175.       if (inkey) {      /* if regular, non-function, non-arrow, key */
  176.          switch(inkey) {
  177.             case 'r' :
  178.             case 'R' : quitflag=2;  /* == don't reset mode on exit */
  179.                        break;
  180.             case ESC : quitflag=1;
  181.                        break;
  182.             default  : /* ignore any other ascii key */
  183.                        break;
  184.          } /* end switch(inkey) */
  185.       } else { /* special key: next character is scan code */
  186.          inkey=getch();
  187.          switch(inkey) {
  188.             case HOME:
  189.                        fy=0; fx=0;
  190.                        if ( pf->l < _screen_length ) {
  191.                           dy=(_screen_length- pf->l)/2;
  192.                           ty=fy+pf->l-1;
  193.                        } else {
  194.                           dy=0;
  195.                           ty=_screen_length-1;
  196.                        }
  197.                        if ( pf->w < _screen_width ) {
  198.                           dx=(_screen_width - pf->w)/2;
  199.                           tx=fx+pf->w-1;
  200.                        } else {
  201.                           dx=0;
  202.                           tx=_screen_width-1;
  203.                        }
  204.                        break;
  205.             case END :
  206.                        fx=0;
  207.                        if ( pf->w < _screen_width ) {
  208.                           dx=(_screen_width - pf->w)/2;
  209.                           tx=fx+pf->w-1;
  210.                        } else {
  211.                           dx=0;
  212.                           tx=_screen_width-1;
  213.                        }
  214.                        if ( pf->l < _screen_length ) {
  215.                           fy=0;
  216.                           dy=(_screen_length- pf->l)/2;
  217.                           ty=fy+pf->l-1;
  218.                        } else {
  219.                           dy=0;
  220.                           fy= pf->l - _screen_length;
  221.                           ty= fy + _screen_length -1;
  222.                        }
  223.                        break;
  224.             case PGUP:
  225.                        if ( pf->w < _screen_width ) {
  226.                           fx=0;
  227.                           dx=(_screen_width - pf->w)/2;
  228.                           tx=fx+pf->w-1;
  229.                        } else {
  230.                           dx=0;
  231.                           fx= pf->w - _screen_width;
  232.                           tx= pf->w - 1;
  233.                        }
  234.                        fy=0;
  235.                        if ( pf->l < _screen_length ) {
  236.                           dy=(_screen_length- pf->l)/2;
  237.                           ty=fy+pf->l-1;
  238.                        } else {
  239.                           dy= 0;
  240.                           ty= fy+_screen_length-1;
  241.                        }
  242.                        break;
  243.             case PGDN:
  244.                        if ( pf->w < _screen_width ) {
  245.                           fx=0;
  246.                           dx=(_screen_width - pf->w)/2;
  247.                           tx=fx+pf->w-1;
  248.                        } else {
  249.                           dx=0;
  250.                           fx= pf->w-_screen_width;
  251.                           tx= fx+_screen_width-1;
  252.                        }
  253.                        if ( pf->l < _screen_length ) {
  254.                           fy=0;
  255.                           dy=(_screen_length- pf->l)/2;
  256.                           ty=fy+pf->l-1;
  257.                        } else {
  258.                           dy=0;
  259.                           fy= pf->l-_screen_length;
  260.                           ty= fy+_screen_length-1;
  261.                        }
  262.                        break;
  263.             case FWD : if ( pf->w > _screen_width ) {
  264.                           dx=0;
  265.                           fx=fx+_screen_width;
  266.                           fx=( fx < pf->w-_screen_width-1) ?
  267.                                     fx : pf->w-_screen_width-1;
  268.                           tx=fx+_screen_width-1;
  269.                        }
  270.                        break;
  271.             case BACK: if ( pf->w > _screen_width ) {
  272.                           dx=0;
  273.                           fx=fx-_screen_width;
  274.                           fx=( fx > 0 ) ? fx : 0;
  275.                           tx=fx+pf->w-1;
  276.                        }
  277.                        break;
  278.             case UP  : if ( pf->l > _screen_length ) {
  279.                           dy=0;
  280.                           fy=fy-_screen_length;
  281.                           fy=(fy > 0 ) ? fy : 0;
  282.                           ty=fy+_screen_length-1;
  283.                        }
  284.                        break;
  285.             case DN  : if ( pf->l > _screen_length ) {
  286.                           dy=0;
  287.                           fy=fy+_screen_length;
  288.                           fy=(fy < pf->l-_screen_length-1) ?
  289.                                                fy : pf->l-_screen_length-1;
  290.                           ty=fy+_screen_length-1;
  291.                        }
  292.                        break;
  293.             case 27 :  quitflag=1;  /* escape? */
  294.                        break;
  295.             default :  quitflag=1;  /* exit on unknown key */
  296.                        break;
  297.          } /* end switch(inkey) */
  298.       } /* end if getch()'s else */
  299. }
  300.  
  301.  
  302.